#include "usbport.h"

UsbPort* UsbPort::m_singleton = NULL;

UsbPort::UsbPort()
{
    //Change port name to the name on your system
    //TODO: find a way to change the friendName of the C-NOTE and make a connections based on this.

    QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
    foreach( QextPortInfo port, ports ) {
        //qDebug() << port.friendName << port.physName << port.vendorID << port.productID << port.enumName << port.portName;
        if( port.vendorID == 1027 && port.productID == 24577 )
        {
            qDebug() << "Found the C-NOTE at " + port.portName + ". Attempting connection ...";
            m_port = new QextSerialPort(port.portName, QextSerialPort::EventDriven);
            break;
        }
    }

    m_port->setBaudRate(BAUD9600);
    m_port->setFlowControl(FLOW_OFF);
    m_port->setParity(PAR_NONE);
    m_port->setDataBits(DATA_8);
    m_port->setStopBits(STOP_2);

    if(m_port->open(QIODevice::ReadWrite) == true)
    {
        qDebug() << "Connection successful.";
        //connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    }
    else
    {
        qDebug() << "Connection failed!!!";
    }
}

UsbPort::~UsbPort()
{
    delete m_port;
}

UsbPort* UsbPort::Instance()
{
    if( m_singleton == NULL )
        m_singleton = new UsbPort();

    return m_singleton;
}

void UsbPort::sendCommand(QByteArray command)
{
    m_port->write(command);
}
